<?php

class My_Validate_RowExists extends Zend_Validate_Abstract {

    const EXISTS = '';

    protected $table;
    protected $column;
    protected $_messageTemplates = array(
        self::EXISTS => "Wartość '%value%' już istnieje. Wybierz inną."
    );

    function __construct($table, $column) {
        $this->table = $table;
        $this->column = $column;
    }

    public function isValid($value) {
        $this->_setValue($value);

        $db = Zend_Db_Table::getDefaultAdapter();
        $row = $db->select()->from($this->table)
                        ->where($this->column . ' = ?', $value)
                        ->query()->fetchAll();
        if ($row) {
            $this->_error(self::EXISTS);
            return false;
        }

        return true;
    }

}
